package com.example.demo.controller.surperController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.ProductLogMapper;
import com.example.demo.base.ArrayCountUtils;
import com.example.demo.base.Result;
import com.example.demo.entity.ProductLog;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.example.demo.base.ArrayCountUtils.merge;

@RestController
@RequestMapping("/productLogSurper")
public class ProductLogSurperController {
    @Resource
    ProductLogMapper productLogMapper;//ProductLog

    /**
     * 获取数据库全部信息
     * @return
     */
    @GetMapping("/all")
    public Result<?> findAll() {
        return Result.success(productLogMapper.selectList(null));
    }

    /**
     * 获取产品物流重量表并去重
     * @return
     */

    @GetMapping("/chatData")
    public Result<?> chatData() {
        List<ProductLog> productLogList = productLogMapper.selectList(null);
        List<ArrayCountUtils.BillsNums> billsNumsList = new ArrayList<>();
        for (ProductLog log : productLogList){
            ArrayCountUtils.BillsNums billsNums = new ArrayCountUtils.BillsNums();
            billsNums.setId(log.getProductName());
            billsNums.setSums(log.getProductWeight());
            billsNumsList.add(billsNums);
            billsNums = null;// 置空强制回收内存，以防数据量大内存泄露
        }
        List<ArrayCountUtils.BillsNums> res = merge(billsNumsList);
        return Result.success(res);
    }

    /**
     * 分页获取数据库数据
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/pageAll")
    public Result<?> findPagePL(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<ProductLog> wrapper = Wrappers.<ProductLog>lambdaQuery();
        Page<ProductLog> farmInfoPage = productLogMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(farmInfoPage);
    }

    /**
     * 根据productId精确查询
     * @param productId
     * @return
     */
    @GetMapping("/findById/{productId}")
    public Result<?> findByProductId(@PathVariable String productId) {
        ProductLog productLog = productLogMapper.selectById(productId);
        List<ProductLog> productLogList = new ArrayList<>();
        if(productLog != null)
        {
            productLogList.add(productLog);
            return Result.success(productLogList);
        }
        return Result.error(0, "没有查询到相关数据");
    }

//    @GetMapping(value = "/pageAll")
//    public Result<?> findPagePL(@RequestParam(defaultValue = "1") Integer pageNum,
//                                @RequestParam(defaultValue = "10") Integer pageSize) {
//        LambdaQueryWrapper<ProductLog> wrapper = Wrappers.<ProductLog>lambdaQuery();
//        Page<ProductLog> farmInfoPage = productLogMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
//        return Result.success(farmInfoPage);
//    }

    /**
     * 根据productName模糊查询
     * @param productName
     * @return
     */
    @GetMapping("/findByProductName")
    public Result<?> findByProductName(@RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "10") Integer pageSize,
                                       @RequestParam String productName) {
        LambdaQueryWrapper<ProductLog> wrapper = Wrappers.<ProductLog>lambdaQuery();
        wrapper.like(ProductLog::getProductName,productName);
        Page<ProductLog> productLogList = productLogMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        if(productLogList.getTotal() > 0)
            return Result.success(productLogList);
        return Result.error(0, "没有查询到相关数据");
    }

    @DeleteMapping("/delById/{productId}")
    public Result<?> delete(@PathVariable String productId) {
        int res = productLogMapper.deleteById(productId);
        if(res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }

    /**
     * 添加信息
     * @param productName
     * @param productId
     * @param productCode
     * @param productWeight
     * @param productSource
     * @param outCharge
     * @param destination
     * @param inCharge
     * @param productBirth
     * @param transporter
     * @param note
     * @return
     */
    @GetMapping(value = "/add")
    public Result<?> saveP(@RequestParam String productName,
                           @RequestParam String productId,
                           @RequestParam String productCode,
                           @RequestParam String productWeight,
                           @RequestParam String productSource,
                           @RequestParam String outCharge,
                           @RequestParam String destination,
                           @RequestParam String inCharge,
                           @RequestParam String productBirth,
                           @RequestParam String expiration,
                           @RequestParam String transporter,
                           @RequestParam String note,
                           @RequestParam String tenantId
                           ) {
        if(expiration.isEmpty()||productName.isEmpty()||productId.isEmpty()||productCode.isEmpty()||productWeight.isEmpty()||productSource.isEmpty()||outCharge.isEmpty()||destination.isEmpty()||inCharge.isEmpty()||productBirth.isEmpty()||transporter.isEmpty())
        {
            return Result.error(0, "请填写完整信息");
        }
        ProductLog productLog = new ProductLog();
        productLog.setProductName(productName);
        productLog.setProductId(Integer.valueOf(productId));
        productLog.setProductCode(productCode);
        productLog.setProductWeight(Double.valueOf(productWeight));
        productLog.setProductSource(productSource);
        productLog.setOutCharge(outCharge);
        productLog.setDestination(destination);
        productLog.setInCharge(inCharge);
        productLog.setProductBirth(productBirth);
        productLog.setExpiration(expiration);
        productLog.setTransporter(transporter);
        productLog.setNote(note);

        productLogMapper.insert(productLog);
        return Result.success();
    }

    /**
     * 根据code更新数据
     * @param productName
     * @param productId
     * @param productCode
     * @param productWeight
     * @param productSource
     * @param outCharge
     * @param destination
     * @param inCharge
     * @param productBirth
     * @param transporter
     * @param note
     * @return
     */
    @GetMapping(value = "/update")
    public Result<?> saveF(@RequestParam String productName,
                           @RequestParam String productId,
                           @RequestParam String productCode,
                           @RequestParam String productWeight,
                           @RequestParam String productSource,
                           @RequestParam String outCharge,
                           @RequestParam String destination,
                           @RequestParam String inCharge,
                           @RequestParam String productBirth,
                           @RequestParam String expiration,
                           @RequestParam String transporter,
                           @RequestParam String note,
                           @RequestParam String tenantId
    ) {
        if(expiration.isEmpty()||productName.isEmpty()||productId.isEmpty()||productCode.isEmpty()||productWeight.isEmpty()||productSource.isEmpty()||outCharge.isEmpty()||destination.isEmpty()||inCharge.isEmpty()||productBirth.isEmpty()||transporter.isEmpty())
        {
            return Result.error(0, "请填写完整信息");
        }
        ProductLog productLog = new ProductLog();
        productLog.setProductName(productName);
        productLog.setProductId(Integer.valueOf(productId));
        productLog.setProductCode(productCode);
        productLog.setProductWeight(Double.valueOf(productWeight));
        productLog.setProductSource(productSource);
        productLog.setOutCharge(outCharge);
        productLog.setDestination(destination);
        productLog.setInCharge(inCharge);
        productLog.setProductBirth(productBirth);
        productLog.setExpiration(expiration);
        productLog.setTransporter(transporter);
        productLog.setNote(note);
        productLogMapper.updateById(productLog);
        return Result.success();
    }

}
